%%%--------------------------------------
%%% @Module  : cus_t
%%% @Description: 测试模块
%%%--------------------------------------

-module(cus_t).


-compile(export_all).


-include("cus.hrl").
-include("cus_pb.hrl").
-include("logger.hrl").


t7() ->
    Url = cus_config:app_api() ++ "/box/v2/genWordsBox",
    Params = "p={\"token\":\"8EF8C3149E43035538DE0E6543FEBEBD\",\"userIds\":[310727,310102,310101],\"spaceId\":\"310727\",\"source\":\"2\",\"paper\":[{\"content\":\"aaa\",\"count\":\"1\"},{\"content\":\"bbb\",\"count\":\"1\"}],\"dev\":\"3\",\"ver\":\"1.5.2\"}",
    cus_util:http_request(Url, Params).


t6() ->
    Url = cus_config:app_api() ++ "/box/v2/genGoldBox",
    Params = "p={\"token\":\"8EF8C3149E43035538DE0E6543FEBEBD\",\"spaceId\":\"310727\",\"userIds\":[310727,310102,310101],\"describe\":\"api test\",\"type\":\"1\",\"source\":\"2\",\"count\":\"2\",\"gold\":\"10\",\"dev\":\"3\",\"ver\":\"1.5.2\"}",
    cus_util:http_request(Url, Params).


t5() ->
    Url = cus_config:app_api() ++ "/gift/v2/sendGift",
    Params = "p={\"token\":\"8EF8C3149E43035538DE0E6543FEBEBD\",\"targetId\":\"310101\",\"spaceId\":\"310727\",\"giftId\":\"1\",\"count\":\"1\",\"dev\":\"1\",\"ver\":\"1.5.2\"}",
    cus_util:http_request(Url, Params).


t4() ->
    Fun = fun() ->
                  lists:foreach(fun(_X) ->
                                        Bin = cus_pb:encode_bound(#bound{type = 3, size = 100}),
                                        cus_pb:decode_bound(Bin)
                                end, lists:seq(1, 100000))
          end,
    timer:tc(Fun).
    


t3() ->
    Mid = "Q_100_10",
    Fun = fun() ->
                  lists:foreach(fun(_X) ->
                                        List = re:split(Mid, "_", [{return, list}]),
                                        Seq = lists:last(List),
                                        string:join(lists:delete(Seq, List), "_")
                                end, lists:seq(1, 10000))
          end,
    timer:tc(Fun).


t2() ->
    #userinfo{uid = "",
              mutenotofication = 0,
              blacks = "",
              follows = [],
              emotions = "",
              mutenotofications = [],
              sticks = []}.


t1(ID) ->
    Url = "http://web.xiaoquan.com/smiley/get_by_id/" ++ ID,
    {ok, {_, _, DataStr}} = httpc:request(Url),
    {ok, Json, _} = rfc4627:decode(DataStr),
    case rfc4627:get_field(Json, "responseCode") of
        {ok, 0} ->
            {ok, Data} = rfc4627:get_field(Json, "data"),
            {ok, Data1} = rfc4627:get_field(Data, "data"),
            Fun = fun(X) ->
                          ?INFO_MSG("x : ~p", [X]),
                          {ok, Name} = rfc4627:get_field(X, "name"),
                          {ok, DisplayOrder} = rfc4627:get_field(X, "display_order"),
                          {ok, FileName} = rfc4627:get_field(X, "file_name"),
                          {ok, Code} = rfc4627:get_field(X, "code"),
                          {ok, Available} = rfc4627:get_field(X, "available"),
                          {ok, EmotionUrl} = rfc4627:get_field(X, "url"),
                          {ok, UrlJpg} = rfc4627:get_field(X, "url_jpg"),
                          Insert = io_lib:format("insert into cus_emotion(packet_id, name, display_order, file_name, code, available, url, url_jpg)
                                                              values(~p, '~s', ~p, '~s', '~s', ~p, '~s', '~s')",
                                                 [list_to_integer(ID),
                                                  binary_to_list(Name),
                                                  list_to_integer(binary_to_list(DisplayOrder)),
                                                  binary_to_list(FileName),
                                                  binary_to_list(Code),
                                                  list_to_integer(binary_to_list(Available)),
                                                  binary_to_list(EmotionUrl),
                                                  binary_to_list(UrlJpg)]),
                          cus_mysql:execute(Insert)
                  end,
            [Fun(Item) || Item <- Data1];
        RespCode ->
            ?ERROR_MSG("REMOTE SERVER ERROR , ResponeseCode : ~p, URL : ~p", [RespCode, Url]),
            []
    end,
    ok.


http_request(Url, Params) ->
    case httpc:request(post, {Url, [], "application/x-www-form-urlencoded" , Params}, [{timeout, 60000}], []) of
        {ok, {_, _, ResBody}} ->
            ResBody;
        {error, Reason} ->
            ?ERROR_MSG("REMOTE SERVER ERROR , REASON : ~p, URL : ~p, Params : ~p", [Reason, Url, Params]),
            {error, Reason}
    end.